const { resolve } = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const { CleanWebpackPlugin } = require('clean-webpack-plugin');
const CopyWebpackPlugin = require('copy-webpack-plugin');


module.exports = {
    target: 'web',
    resolve: {
        extensions: ['.js', '.json', '.ts', '.jsx', '.vue'],
        mainFiles: ['index'],
        modules: ['node_modules'],
        alias: {
            '@': resolve(__dirname, 'src')
        }
    },
    mode: 'development',
    entry: './src/index.ts',
    output: {
        filename: 'js/build.js',
        path: resolve(__dirname, 'dist'),
        environment: {
            // 告诉webpack在打包的时候不要使用箭头函数与const
            arrowFunction: false,
            const: false
        }
    },
    // 指定webpack打包时要使用的模块
    module: {
        rules: [{
                test: /\.ts$/,
                exclude: /node_modules/,
                use: [
                    'babel-loader',
                    'ts-loader',
                ]
            },
            {
                test: /\.less$/,
                use: [
                    'style-loader',
                    'css-loader',
                    'postcss-loader',
                    'less-loader'
                ]
            },
            {
                test: /\.js$/,
                exclude: /node_modules/,
                use: [
                    'babel-loader'
                ]
            }
        ]
    },
    plugins: [
        new HtmlWebpackPlugin({
            title: 'GreedySnake',
            template: './public/index.html'
        }),
        new CleanWebpackPlugin(),
        new CopyWebpackPlugin({
            patterns: [{
                from: 'public',
                to: resolve(__dirname, 'dist'),
                globOptions: {
                    ignore: ['**/index.html']
                }
            }]
        })
    ],

}